package com.example.petshopping.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.petshopping.R;
import com.example.petshopping.model.Product;

import java.util.List;

public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ViewHolder> {

    private final Context context;
    private final List<Product> products;
    private OnProductClickListener listener;

    public ProductAdapter(Context context, List<Product> products) {
        this.context = context;
        this.products = products;
    }

    public interface OnProductClickListener {
        void onProductClick(Product product, int position);
        void onFavoriteClick(Product product, int position);
        void onDeleteClick(Product product, int position);
    }

    public void setOnProductClickListener(OnProductClickListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_product, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Product product = products.get(position);
        
        // 设置商品数据
        holder.titleView.setText(product.getTitle());
        holder.priceView.setText(product.getPrice());
        holder.imageView.setImageResource(product.getImageResource());
        holder.timeView.setText(product.getAuctionEndTime());
        holder.sellerView.setText(product.getSeller());
        holder.sellerAvatarView.setImageResource(product.getSellerAvatarResource());
        
        // 设置收藏按钮状态
        if (product.isFavorite()) {
            holder.favoriteView.setImageResource(R.drawable.ic_heart_filled);
        } else {
            holder.favoriteView.setImageResource(R.drawable.ic_heart_outline);
        }
        
        // 精品标签
        if (product.isPromoted()) {
            holder.promotedView.setVisibility(View.VISIBLE);
        } else {
            holder.promotedView.setVisibility(View.GONE);
        }
        
        // 设置点击事件
        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onProductClick(product, holder.getAdapterPosition());
            }
        });
        
        holder.favoriteView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onFavoriteClick(product, holder.getAdapterPosition());
            }
        });
        
        holder.deleteView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onDeleteClick(product, holder.getAdapterPosition());
            }
        });
    }

    @Override
    public int getItemCount() {
        return products.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView titleView;
        TextView priceView;
        TextView timeView;
        TextView sellerView;
        ImageView sellerAvatarView;
        ImageView favoriteView;
        ImageView deleteView;
        View promotedView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.product_image);
            titleView = itemView.findViewById(R.id.product_title);
            priceView = itemView.findViewById(R.id.product_price);
            timeView = itemView.findViewById(R.id.product_time);
            sellerView = itemView.findViewById(R.id.seller_name);
            sellerAvatarView = itemView.findViewById(R.id.seller_avatar);
            favoriteView = itemView.findViewById(R.id.favorite_button);
            deleteView = itemView.findViewById(R.id.delete_button);
            promotedView = itemView.findViewById(R.id.product_badge);
        }
    }
} 